package edu.cmu.ece.ds.droidchat.controller;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.Toolbox;
import edu.cmu.ece.ds.droidchat.model.UserInfo;
import edu.cmu.ece.ds.droidchat.widget.ActionItem;
import edu.cmu.ece.ds.droidchat.widget.MyQuickAction;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList.OnListClickListener;
import greendroid.widget.QuickActionBar;
import greendroid.widget.QuickActionWidget;
import greendroid.widget.QuickActionWidget.OnQuickActionClickListener;

public class UserListBaseAdapter extends BaseAdapter {
    private final boolean useVerticalQuickAction = true;
    private ArrayList<UserInfo> userInfoArray;
    private LayoutInflater mInflater;

    public UserListBaseAdapter(Context context,
            ArrayList<UserInfo> userInfoArray) {
        this.userInfoArray = userInfoArray;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return userInfoArray.size();
    }

    @Override
    public Object getItem(int position) {
        return userInfoArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.nearby_users_list_item,
                    null);
            holder = new ViewHolder();
            holder.iconImageView = (ImageView) convertView
                    .findViewById(R.id.user_image);
            holder.nicknameTextView = (TextView) convertView
                    .findViewById(R.id.user_nickname);
            holder.statusTextView = (TextView) convertView
                    .findViewById(R.id.user_status);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final UserInfo uInfo = userInfoArray.get(position);
        if (uInfo.getProfilePicture() == null) {
            Toolbox.imageDownloader.downloadToUserInfo(
                    uInfo.getProfilePictureURLString(), uInfo,
                    holder.iconImageView);
        } else {
            holder.iconImageView.setImageDrawable(uInfo.getProfilePicture());
        }
        final ImageView cView = holder.iconImageView;
        if (!useVerticalQuickAction) {
            holder.iconImageView.setOnClickListener(new OnClickListener() {
                private QuickActionBar actionBar;

                @Override
                public void onClick(View v) {
                    actionBar = new QuickActionBar(cView.getContext(),
                            R.drawable.action_bar_front_layer_background);

                    actionBar.addQuickAction(new MyQuickAction(cView
                            .getContext(), R.drawable.new_session,
                            R.string.create_new_session));
                    actionBar.addQuickAction(new MyQuickAction(cView
                            .getContext(), R.drawable.invite,
                            R.string.invite_to_existing_session));
                    actionBar.addQuickAction(new MyQuickAction(cView
                            .getContext(), R.drawable.info, R.string.info));
                    actionBar.addQuickAction(new MyQuickAction(cView
                            .getContext(), R.drawable.block, R.string.block));
                    actionBar
                            .setOnQuickActionClickListener(new UserLogoOnQuickActionClickListener(
                                    uInfo));

                    holder.iconImageView
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    actionBar.show(cView);
                                }
                            });

                }
            });
        } else {
            // useVerticalQuickAction == true

            cView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    QuickActionList l = new QuickActionList(v,
                            LinearLayout.HORIZONTAL);

                    l.addActionItem(new ActionItem(cView.getContext(),
                            R.drawable.new_session, R.string.create_new_session,
                            LinearLayout.VERTICAL));
                    l.addActionItem(new ActionItem(cView.getContext(),
                            R.drawable.invite,
                            R.string.invite_to_existing_session,
                            LinearLayout.VERTICAL));
                    l.addActionItem(new ActionItem(cView.getContext(),
                            R.drawable.info, R.string.info,
                            LinearLayout.VERTICAL));
                    l.addActionItem(new ActionItem(cView.getContext(),
                            R.drawable.block, R.string.block,
                            LinearLayout.VERTICAL));
                    l.setOnListClickListener(new VerticalListOnClickListener(
                            uInfo));

                    l.show();
                }
            });
        }
        holder.nicknameTextView.setText(uInfo.getNickName());
        holder.statusTextView.setText(uInfo.getStatus());

        return convertView;
    }

    static class ViewHolder {
        ImageView iconImageView;
        TextView nicknameTextView;
        TextView statusTextView;
    }

    // These two listeners should be the same as they just corresponds to the
    // same behavior in two different modes (useVerticalQuickAction)
    private class UserLogoOnQuickActionClickListener implements
            OnQuickActionClickListener {

        private UserInfo mUserInfo;

        public UserLogoOnQuickActionClickListener(UserInfo uInfo) {
            mUserInfo = uInfo;
        }

        public void onQuickActionClicked(QuickActionWidget widget, int position) {
            switch (position) {
            case 0:
                Toast.makeText(widget.getContentView().getContext(),
                        "new chat with " + mUserInfo.getNickName(),
                        Toast.LENGTH_SHORT).show();

                Toolbox.createChatWindowActivity(widget.getContentView()
                        .getContext(), mUserInfo);
                break;
            case 1:
                Toast.makeText(widget.getContentView().getContext(),
                        "invite " + mUserInfo.getNickName(), Toast.LENGTH_SHORT)
                        .show();
                break;
            case 2:
                Toast.makeText(widget.getContentView().getContext(),
                        "view info of " + mUserInfo.getNickName(),
                        Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(widget.getContentView().getContext(),
                        "block " + mUserInfo.getNickName(), Toast.LENGTH_SHORT)
                        .show();
                break;
            }
        }
    }

    private class VerticalListOnClickListener implements OnListClickListener {
        private UserInfo mUserInfo;

        public VerticalListOnClickListener(UserInfo uInfo) {
            mUserInfo = uInfo;
        }

        @Override
        public void onQuickActionListClicked(View contentView,
                int position) {
            switch (position) {
            case 0:
                Toast.makeText(contentView.getContext(),
                        "new chat with " + mUserInfo.getNickName(),
                        Toast.LENGTH_SHORT).show();

                Toolbox.createChatWindowActivity(contentView.getContext(),
                        mUserInfo);
                break;
            case 1:
                Toast.makeText(contentView.getContext(),
                        "invite " + mUserInfo.getNickName(), Toast.LENGTH_SHORT)
                        .show();
                break;
            case 2:
                Toast.makeText(contentView.getContext(),
                        "view info of " + mUserInfo.getNickName(),
                        Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(contentView.getContext(),
                        "block " + mUserInfo.getNickName(), Toast.LENGTH_SHORT)
                        .show();
                break;
            }
        }
    }
}
